//
// Created by tang on 2020/6/17.
//

#ifndef TESTREMOTE_GPIO_UTILITY_HPP
#define TESTREMOTE_GPIO_UTILITY_HPP

#include <pigpio.h>

class DigitalPin {
    const unsigned pin_;
public:
    explicit DigitalPin(unsigned pin, int mode = PI_OUTPUT, int pull = PI_PUD_OFF);

    void operator=(bool val) const;

    [[nodiscard]]
    operator bool() const;

    void toggle() const;
};

class PWMPin {
    const unsigned pin_;
    int freq_;
    int range_;
public:
    explicit PWMPin(unsigned pin, unsigned freq = 2000, unsigned range = 256);

    unsigned setFrequency(unsigned frequency);

    unsigned setRange(unsigned range);

    void setDutyCycle(double duty_cycle) const;

    void operator=(double val) const;
};

class IIC
{
    DigitalPin scl_;
    DigitalPin sda_;
    const char addr_;
public:
    IIC(unsigned scl, unsigned sda, unsigned addr);

    void write(const char *bytes, unsigned len);
    void read(char *bytes, unsigned len);
private:
    void start();
    void stop();
    void writeByte(char byte);
    void readByte(char &byte);
};

#endif //TESTREMOTE_GPIO_UTILITY_HPP
